package com.business.commons.baseUtil;

import java.util.Date;

import com.business.commons.date.TimeUtil;

/**
 * 验证通用
 * @author ddr
 *
 */
public class ValidUtil {

	private String  defaultFmt = "yyyy-MM-dd";
	
	private ValidUtil(){
	}
	
	
	private static ValidUtil instance;
	
	public static  ValidUtil get() {
		if(instance == null ){
			instance = new ValidUtil(); 
		}
		return instance;
	}
	/***
	 * 验正是否全是数字
	 * @param str
	 * @return
	 */
	public  boolean isDigit(String str ){
		return str.matches("^\\d{1,}$");
	}
	
	
	/***
	 * 验正是否全是数字
	 * @param str
	 * @return
	 */
	public   boolean isDigit(String str,int len ){
		return str.matches("^\\d{"+len+"}$");
	}
	
	/***
	 * 验正是否全是数字
	 * @param str
	 * @return
	 */
	public   boolean isInt(String str ){
		return str.matches("^(-)?\\d{1,}$");
	}
	
	/**
	 * 11位手机号 1开头
	 * @param mobile
	 * @return
	 */
	public  boolean isMobile(String mobile){
		return mobile.matches("^1\\d{10}$");
	}
	
	
	/**
	 * email
	 * @param eamil
	 * @return
	 */
	public  boolean isEmail(String eamil){
		String check = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"; 
		return eamil.matches(check);
	}
	
	
	/**
	 * 验证带精确到几位小数的数字 
	 * @param doubleStr
	 * @param scale 
	 * @return
	 */
	public  boolean isNumber(String doubleStr,int scale){
		String check = "^(-)?\\d{1,}(\\.\\d{1,"+scale+"})?$"; 
		return doubleStr.matches(check);
	}
	
	/**
	 * 验证只要是数字即可
	 * 2.115 正确
	 * 2 正确 
	 * @param doubleStr
	 * @param scale
	 * @return
	 */
	public  boolean isNumber(String doubleStr){
		String check = "^(-)?\\d{1,}(\\.\\d{1,})?$"; 
		return doubleStr.matches(check);
	}
	
	/**
	 * 
	 * isDate:(这里用一句话描述这个方法的作用). <br/> 
	 * 判断日期格式是否正确.<br/> 
	 * @author ddr 
	 * @param date 
	 * @param fmt
	 * @return
	 */
	public boolean isDate(String date,String fmt){
		Date d = TimeUtil.get().parseDate(fmt, date);
		return d!=null;
	}
	
	
	/**
	 * 
	 * isDate:(这里用一句话描述这个方法的作用). <br/> 
	 * 判断日期格式是否正确.<br/> 
	 * @author ddr 
	 * @param date 
	 * @param fmt
	 * @return
	 */
	public boolean isDate(String date){
		
		Date d = TimeUtil.get().parseDate(defaultFmt, date);
		return d!=null;
	}
	
	
	
	
	public boolean containsEmoji(String str){
		if(str == null ){
			return false;
		}
		
			
		
		for(char c: str.toCharArray()){
			 System.out.print((int) c+" \t");
		}
		return false;
		
	}
	
	
	
	/**
	 * 判断是否全是中文
	* @Title: isChinese 
	* @param @param str
	* @param @return    设定文件 
	* @return boolean    返回类型 
	* @throws
	 */
	public static boolean isChinese(String str) {
	    boolean result = false;
	    if(str!=null && str.length()>0){
	    	for(char c : str.toCharArray()){
	    		 if (c >= 19968 && c <= 171941) {// 汉字范围 \u4e00-\u9fa5 (中文)
	    		       continue;
	    		   }
	    		 else{
	    			 return false;
	    		 }
	    	}
	    	result = true;
	    }
	    return result;
	}
	
	
	
	
	
	public static void main(String[] args) {
		//
		ValidUtil vlu = ValidUtil.get();
		System.out.println(vlu.isDigit("124"));
		System.out.println(vlu.isMobile("13761123436"));
		System.out.println(vlu.isEmail("ddr2@foxmail.com")); 
		System.out.println(vlu.isEmail("taoqi_2000@163.com")); 
		System.out.println(vlu.isNumber("2.11",2)); 
		System.out.println(vlu.isNumber("2"));
		
		System.out.println(vlu.isDate("1013-29-02"));
		
		
		System.out.println(vlu.containsEmoji("😊😜😏😔😣1"));
		
		System.out.println(isChinese("中国"));
		System.out.println(isChinese("中国A"));
	}
	
}
